import VehicleFactory from "./VehicleFactory"
import TrafficParticipants from "./TrafficParticipants"
import { useRouter } from "vue-router"
class VehicleAccessManager {
  constructor ({ map, areaId, startTime, endTime }) {
    this.map = map
    startTime = 1662351000000;
    endTime = 1662352000000;
    // startTime = 1663041600000
    // endTime = 1663041900000
    this.startTime = startTime
    this.endTime = endTime
    this.areaId = areaId
    this.initClock(startTime, endTime)
    this.vf = new VehicleFactory({ map, url: window.g.vfSocket, startTime, endTime })
    this.tp = new TrafficParticipants({ map, url: window.g.tpSocket, areaId, startTime, endTime })
    const handler = new Cesium.ScreenSpaceEventHandler(map.scene.canvas)
    const router = useRouter()
    handler.setInputAction((evt) => {
      const pick = map.scene.pick(evt.position)
      if (Cesium.defined(pick) && pick.id) {
        const entity = pick.id
        const vehicle1 = this.vf.vehicleHolder[entity.id]
        const vehicle2 = this.tp.vehicleHolder[entity.id]
        if (vehicle1 || vehicle2) {
          router.push({
            path: "/Car",
            query: {
              id: entity.id,
              tableType: entity.properties["type"].getValue(),
              startTime: this.startTime,
              endTime: this.endTime,
              areaId: this.areaId
            },
          })
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
  }
  /**
   * 初始化时间轴
   * @param {number} startTime 起始时间
   * @param {number} endTime 结束时间
   */
  initClock (startTime, endTime) {
    console.log("TrafficParticipants：初始化时间轴！     " + startTime + "----" + endTime)
    this.startTime = startTime
    this.endTime = endTime
    const jd_start = Cesium.JulianDate.fromDate(new Date(startTime))
    const js_end = Cesium.JulianDate.fromDate(new Date(endTime))
    this.map.clock.startTime = jd_start.clone()
    this.map.clock.currentTime = jd_start.clone()
    this.map.clock.stopTime = js_end.clone()
    this.map.clock.clockRange = Cesium.ClockRange.CLAMPED
    this.map.clock.shouldAnimate = false
  }

  sendQueryMessage (startTime, endTime) {
    this.initClock(startTime, endTime)
    this.vf.sendQueryMessage(startTime, endTime, true)
    this.tp.sendQueryMessage(startTime, endTime, true)
  }

  removeAll () {
    this.vf.removeAllCar()
    this.tp.removeAllCar()
  }

  setVisible (visible) {
    this.vf.controlAllVehiclesVisible(visible)
    this.tp.controlAllVehiclesVisible(visible)
  }

  changeArea (areaId) {
    this.areaId = areaId;
    this.tp.areaId = areaId;
  }

  destory () {
    this.vf.destroy();
    this.tp.destory();
  }
}
export default VehicleAccessManager
